/**
 * 
 */
package com.dream.utils.bean;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

import com.dream.utils.date.DateParseUtils;
import com.dream.utils.exception.BusinessException;
import com.dream.utils.log.DreamLogUtils;

/**
 * @copyright evan
 * @author evan
 * @date 2013-9-4
 */
public class MyBeanUtils extends BeanUtils {
	
	 static {  
	        ConvertUtils.register(new MyDateConvert(), java.util.Date.class);  
	        ConvertUtils.register(new MyDateConvert(), java.sql.Date.class);  
	    }  
	 
	/**
	 * filter bean copy exception
	 * @copyright evan
	 * @author evan
	 * @date 2013-9-4
	 * @param dest
	 * @param orig
	 */
	public static void copyProperties(Object dest, Object orig){
		
		if(dest == null && orig == null){
			DreamLogUtils.error(MyBeanUtils.class, "copyProperties", "argument is illegal", null);
			throw new BusinessException("MyBeanUtils.copyProperties's arguments is null");
		}
		
		try {
			BeanUtils.copyProperties(dest, orig);
			
		} catch (IllegalAccessException e) {
			DreamLogUtils.error(MyBeanUtils.class, "copyProperties", dest.getClass().getSimpleName(), e);
			throw new BusinessException("MyBeanUtils.copyProperties's IllegalAccessException occred");
		} catch (InvocationTargetException e) {
			DreamLogUtils.error(MyBeanUtils.class, "copyProperties", dest.getClass().getSimpleName(), e);
			throw new BusinessException("MyBeanUtils.copyProperties's InvocationTargetException occured");
		}
	}
	
	/**
	 * 
	 * @copyright evan
	 * @author evan
	 * @date 2013-9-5
	 * @param bean
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static String toString(Object bean){
		
		String result = "";
		if(bean == null ){
			DreamLogUtils.error(MyBeanUtils.class, "toString", "argument is illegal", null);
			throw new BusinessException("MyBeanUtils.copyProperties's arguments is null");
		}
		
		try {
			
			Map describe = BeanUtils.describe(bean);
			StringBuilder builder = new StringBuilder("bean property{");
			for(Object key : describe.keySet()){
				builder.append(key.toString()).append("=");
				builder.append(describe.get(key).toString()).append(";");
			}
			builder.append("}");
			result = builder.toString();
		} catch (IllegalAccessException e) {
			DreamLogUtils.error(MyBeanUtils.class, "toString", bean.getClass().getSimpleName(), e);
			throw new BusinessException("MyBeanUtils.toString's IllegalAccessException occred");
		} catch (InvocationTargetException e) {
			DreamLogUtils.error(MyBeanUtils.class, "toString", bean.getClass().getSimpleName(), e);
			throw new BusinessException("MyBeanUtils.toString's InvocationTargetException occred");
		} catch (NoSuchMethodException e) {
			DreamLogUtils.error(MyBeanUtils.class, "toString", bean.getClass().getSimpleName(), e);
			throw new BusinessException("MyBeanUtils.toString's NoSuchMethodException occred");
		}
		return result;
	}
	
	private static class MyDateConvert implements Converter {  
		  
	    public Object convert(@SuppressWarnings("rawtypes") Class arg0, Object arg1) {  
	    	
	    	Date result = null;
	        if(arg1 != null){  
	        	String p = arg1.toString();  
	        	if(p.contains(".")){
	        		p = p.substring(0, p.indexOf("."));
	        		result = DateParseUtils.parseDate(p); 
	        	}
	        	
	        }
	        return result;
	    }  
	}  
}
